﻿CREATE PROCEDURE [dbo].[proc_Customer_Getlist_Audit_141014]
(
	@CompanyId int,
	@Stext nvarchar(50),
	@StartIndex int,
	@EndIndex int
)
As
Begin
	Declare @cSql nvarchar(3000)

	Set @cSql = ''
	If(@Stext<>'')	--关键词为空
		Set @cSql=' And CustomerName Like ''%'+@Stext+'%'' '

	Set @cSql=';WITH List As(Select ROW_NUMBER() OVER (ORDER BY CreateDate,ProtectId DESC) AS Row,
		Id,CustomerId,CustomerName,MnemonicWorks,Tel,CreateName,CreateDate,ProtectId
		From Customer
		Where CompanyId='+Convert(varchar(10),@CompanyId)+' And Audit=0'+@cSql+'
	)

	Select *,
	(Select Top 1 EName From Employee Where Employee.UserName=List.CreateName) As CreateNameEName,
	(Select Top 1 Title From Customer_Protect c Where c.Id=List.ProtectId) As Protect,
	(Select Count(0) From List) As RecordCount
	From List
	Where Row Between '+Convert(varchar(10),@StartIndex)+' And '+Convert(varchar(10),@EndIndex)+'
	Order By Row'

	Exec(@cSql)
End
